package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.BidDetailsActivity;
import com.kkd.kkdapp.activity.MyDebtDetailActivity;
import com.kkd.kkdapp.activity.MyPlanDetailsActivity;
import com.kkd.kkdapp.http.entity.CalendarListDataItem;
import com.kkd.kkdapp.http.entity.RepaymentHistoryDataBean;
import com.kkd.kkdapp.http.entity.RepaymentHistoryHeadData;
import com.kkd.kkdapp.listener.OnSingleClickListener;

public class RepaymentHistoryAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater inflater;
	private List<RepaymentHistoryDataBean> list;
	private DecimalFormat df_point2;

	public RepaymentHistoryAdapter(Context context,
			List<RepaymentHistoryDataBean> list) {
		mContext = context;
		this.list = list;
		inflater = LayoutInflater.from(mContext);
		df_point2 = new DecimalFormat("###,##0.00");
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		return list.get(position).getType();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		HeadViewHolder headViewHolder = null;
		ItemViewHolder itemViewHolder = null;
		if (convertView == null) {
			switch (type) {
			case RepaymentHistoryDataBean.HEAD:
				headViewHolder = new HeadViewHolder();
				convertView = inflater.inflate(
						R.layout.head_repaymenthistory_listview, parent, false);
				headViewHolder.tv_desc = (TextView) convertView
						.findViewById(R.id.tv_desc);
				convertView.setTag(headViewHolder);
				break;

			case RepaymentHistoryDataBean.ITEM:
				itemViewHolder = new ItemViewHolder();
				convertView = inflater.inflate(
						R.layout.item_curday_calendar_listview, parent, false);
				itemViewHolder.head_title = convertView
						.findViewById(R.id.head_title);
				itemViewHolder.tv_title = (TextView) convertView
						.findViewById(R.id.tv_title);
				itemViewHolder.tv_status = (TextView) convertView
						.findViewById(R.id.tv_status);
				itemViewHolder.tv_num = (TextView) convertView
						.findViewById(R.id.tv_num);
				itemViewHolder.tv_num_title = (TextView) convertView
						.findViewById(R.id.tv_num_title);
				itemViewHolder.tv_poidate = (TextView) convertView
						.findViewById(R.id.tv_poidate);
				itemViewHolder.tv_poiterm = (TextView) convertView
						.findViewById(R.id.tv_poiterm);
				itemViewHolder.tv_poiterm_company = (TextView) convertView
						.findViewById(R.id.tv_poiterm_company);
				itemViewHolder.toDetail = convertView
						.findViewById(R.id.toDetail);
				itemViewHolder.line_bottom = convertView
						.findViewById(R.id.line_bottom);
				convertView.setTag(itemViewHolder);
				break;
			}
		} else {
			switch (type) {
			case RepaymentHistoryDataBean.HEAD:
				headViewHolder = (HeadViewHolder) convertView.getTag();
				break;

			case RepaymentHistoryDataBean.ITEM:
				itemViewHolder = (ItemViewHolder) convertView.getTag();
				break;
			}
		}
		/*
		 * 添加数据
		 */
		switch (type) {
		case RepaymentHistoryDataBean.HEAD:
			RepaymentHistoryHeadData headData = list.get(position)
					.getHeadData();
			if (headData != null) {
				headViewHolder.tv_desc.setText(headData.getMonthStat());
			}
			break;

		case RepaymentHistoryDataBean.ITEM:
			if (position == list.size() - 1) {
				itemViewHolder.line_bottom.setVisibility(View.GONE);
			} else {
				itemViewHolder.line_bottom.setVisibility(View.VISIBLE);
			}

			final CalendarListDataItem itemData = list.get(position)
					.getItemData();
			if (itemData != null) {
				// 标题
				itemViewHolder.tv_title.setText(itemData.getFinanceTitle());
				// 状态
				final String billStatusDesc = itemData.getBillStatusDesc();
				itemViewHolder.tv_status.setText(billStatusDesc);
				if ("招标中".equals(billStatusDesc)) {
					itemViewHolder.tv_status
							.setBackgroundResource(R.drawable.bidding_bg);
				} else if ("已满标".equals(billStatusDesc)) {
					itemViewHolder.tv_status
							.setBackgroundResource(R.drawable.bidding_bg);
				} else if ("还款中".equals(billStatusDesc)) {
					itemViewHolder.tv_status
							.setBackgroundResource(R.drawable.repaymentin_bg);
				} else if ("已还清".equals(billStatusDesc)) {
					itemViewHolder.tv_status
							.setBackgroundResource(R.drawable.haspaidoff_bg);
				} else {
					itemViewHolder.tv_status
							.setBackgroundResource(R.drawable.repaymentin_bg);
				}
				// 金额
				// 本金
				long principal = itemData.getExpectedPrincipal();
				// 利息
				long interest = itemData.getExpectedInterest()
						+ itemData.getExpectedExtraInterest()
						+ itemData.getExpectedCouponInterest();
				if (principal > 0 && interest == 0) {
					itemViewHolder.tv_num_title.setText("本金还款(元)");
				} else if (principal == 0 && interest > 0) {
					itemViewHolder.tv_num_title.setText("利息还款(元)");
				} else if (principal > 0 && interest > 0) {
					itemViewHolder.tv_num_title.setText("本、息还款(元)");
				}
				itemViewHolder.tv_num.setText(df_point2
						.format((principal + interest) * 0.01));
				// 投资日期
				itemViewHolder.tv_poidate.setText(itemData.getApplyTimeDesc());
				// 投资期限
				String termDesc = itemData.getTermDesc();
				if (!TextUtils.isEmpty(termDesc)) {
					if (termDesc.contains("个")) {
						itemViewHolder.tv_poiterm.setText(termDesc.substring(0,
								termDesc.indexOf("个")));
						itemViewHolder.tv_poiterm_company.setText(termDesc
								.substring(termDesc.indexOf("个")));
					} else if (termDesc.contains("天")) {
						itemViewHolder.tv_poiterm.setText(termDesc.substring(0,
								termDesc.indexOf("天")));
						itemViewHolder.tv_poiterm_company.setText(termDesc
								.substring(termDesc.indexOf("天")));
					} else {
						itemViewHolder.tv_poiterm.setText(termDesc);
						itemViewHolder.tv_poiterm_company.setText("");
					}
				} else {
					itemViewHolder.tv_poiterm.setText("");
					itemViewHolder.tv_poiterm_company.setText("");
				}
				// 跳转到投资详情页
				itemViewHolder.toDetail
						.setOnClickListener(new OnSingleClickListener() {

							@Override
							public void onSingleClick(View v) {
								String orderId = itemData.getOrderId();
								Intent intent;
								if (itemData.getIsFinance() == 1) { // K计划
									intent = new Intent(mContext,
											MyPlanDetailsActivity.class);
									intent.putExtra("poiId", orderId);
								} else if (itemData.getIsVirtualBid() == 1) {// 体验标
									intent = new Intent(mContext,
											BidDetailsActivity.class);
									intent.putExtra("poiId", orderId);
									intent.putExtra("isTiyan", true);
								} else {
									intent = new Intent(mContext,
											MyDebtDetailActivity.class);
									intent.putExtra("poi_id", orderId);
									intent.putExtra("type", "已还清"
											.equals(billStatusDesc) ? 4 : 1);
								}
								mContext.startActivity(intent);
							}
						});
			}
			break;
		}
		return convertView;
	}

	class HeadViewHolder {
		TextView tv_desc;
	}

	class ItemViewHolder {
		View head_title;
		TextView tv_title;
		TextView tv_status;
		TextView tv_num;
		TextView tv_num_title;
		TextView tv_poidate;
		TextView tv_poiterm;
		TextView tv_poiterm_company;
		View toDetail;
		View line_bottom;
	}
}
